/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.incad.psh.pshassign.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.Widget;
import cz.incad.psh.pshassign.client.rpc.PSHService;
import cz.incad.psh.pshassign.client.rpc.PSHServiceAsync;
import static com.google.gwt.query.client.GQuery.$;
import static gwtquery.plugins.ui.Ui.Ui;
import java.util.Date;

/**
 *
 * @author alberto
 */
public class Admin extends Composite {
    
    private static AdminUiBinder uiBinder = GWT.create(AdminUiBinder.class);
    
    
    PSHServiceAsync pshService = GWT.create(PSHService.class);
    MainPage mainPage; 
    
    @UiField
    HTMLPanel content;
    @UiField
    FocusPanel full_index;
    @UiField
    FocusPanel update_index;
    @UiField
    FocusPanel index_psh;
    @UiField
    FocusPanel update_psh;
    @UiField
    FocusPanel assign;
    @UiField
    FocusPanel update_assign;
    @UiField
    FocusPanel institutions;
    @UiField
    AbsolutePanel menu;
    
    interface AdminUiBinder extends UiBinder<Widget, Admin> {
    }
    
    @Override
    protected void onLoad(){
        mainPage = (MainPage) RootLayoutPanel.get().getWidget(0);
        menu.setVisible(false);
        update_psh.setVisible(false);
    }
    
    public void hide(){
        menu.setVisible(false);
    }
    
    public void show(){
        menu.setVisible(true);
    }
    
    public void toggle(){
        menu.setVisible(!menu.isVisible());
    }
    
    public Admin() {
        initWidget(uiBinder.createAndBindUi(this));
        
        
        full_index.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                if (full_index.getStyleName().contains("indexing")) {
                    pshService.cancelIndex(new AsyncCallback<Void>() {
                        @Override
                        public void onFailure(Throwable caught) {
                            Window.alert("Error: " + caught.getLocalizedMessage());
                        }

                        @Override
                        public void onSuccess(Void result) {
                            Window.alert("Proccess will be cancelled");
                        }
                    });

                } else {
                    ((Label)full_index.getWidget()).setText("cancel index");
                    full_index.setStyleName("indexing", true);
                    mainPage.initIndexProgress();
                    pshService.fullIndex(new AsyncCallback() {
                        @Override
                        public void onFailure(Throwable caught) {
                            Window.alert("Error: " + caught.getLocalizedMessage());
                            ((Label)full_index.getWidget()).setText("full index invenio");
                            full_index.removeStyleName("indexing");
                        }

                        @Override
                        public void onSuccess(Object result) {
                            Window.alert((String) result);
                            ((Label)full_index.getWidget()).setText("full index invenio");
                            full_index.removeStyleName("indexing");
                        }
                    });
                }
                hide();
            }
        });
        update_index.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                String today = DateTimeFormat.getFormat("yyyy-MM-dd").format(new Date());
                String from = Window.prompt("from date (YYYY-MM-DD)", today);
                if(from==null){
                    return;
                }
                mainPage.initIndexProgress();
                pshService.updateIndex(from, new AsyncCallback() {
                    @Override
                    public void onFailure(Throwable caught) {
                        Window.alert("Error: " + caught.getLocalizedMessage());
                    }

                    @Override
                    public void onSuccess(Object result) {
                        Window.alert((String) result);
                    }
                });
                hide();
            }
        });
        index_psh.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                pshService.indexPsh(new AsyncCallback() {
                    @Override
                    public void onFailure(Throwable caught) {
                        Window.alert("Error: " + caught.getLocalizedMessage());
                    }

                    @Override
                    public void onSuccess(Object result) {
                        Window.alert((String) result);
                    }
                });
                hide();
            }
        });
        update_psh.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                String url = Window.prompt("PSH url", "");
                if (url.trim().length() > 0) {
                    pshService.updatePsh(url, new AsyncCallback() {
                        @Override
                        public void onFailure(Throwable caught) {
                            Window.alert("Error: " + caught.getLocalizedMessage());
                        }

                        @Override
                        public void onSuccess(Object result) {
                            Window.alert((String) result);
                        }
                    });
                }
                hide();
            }
        });
        assign.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                if(mainPage==null){
                    mainPage = (MainPage) RootLayoutPanel.get().getWidget(0);
                }
                mainPage.initAssignProgress();
                pshService.assign(false, new AsyncCallback() {
                    @Override
                    public void onFailure(Throwable caught) {
                        Window.alert(caught.getLocalizedMessage());
                    }

                    @Override
                    public void onSuccess(Object result) {
                        Window.alert((String) result);
                    }
                });
                hide();
            }
        });
        update_assign.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                if(mainPage==null){
                    mainPage = (MainPage) RootLayoutPanel.get().getWidget(0);
                }
                mainPage.initAssignProgress();
                pshService.assign(true, new AsyncCallback() {
                    @Override
                    public void onFailure(Throwable caught) {
                        Window.alert(caught.getLocalizedMessage());
                    }

                    @Override
                    public void onSuccess(Object result) {
                        Window.alert((String) result);
                    }
                });
                hide();
            }
        });
        
        institutions.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                $("#institutions").as(Ui).dialog("{width:600, height: 400}");
            }
        });
    }
}
